<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class UserFbLogModel extends Model {

    protected $table ="user_fb_log";

    protected $pk = 'id';

    private $cacheKey="user:fb:log:id:";

    private $cacheTime=5184000;


    /*
     * 获取total
     * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    public function getUserTotalFb($uid,$format = true){
        $where = [];
        $where[] = ["uid","=",$uid];
        $where[] = ["fb_status","=",1];
        $fbAmount = Db::name($this->table)->where($where)->sum("fb");
        if($format){
            return  round(  $fbAmount / 100,2);
        }else{
            return  $fbAmount;
        }
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "DESC"){

        $obj =  Db::name($this->table)->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $list = $list->toArray();
            foreach ($list as &$item){
                $item["fb"] =  round(  $item["fb"] / 100,2);
            }
            return $list;
        }
        return [];
    }




    public function batchAdd($list){
        return Db::name($this->table)->insertAll($list);
    }



    /*
     *
     * */
    public function updateAll($where,$data){
        return Db::name($this->table)->where($where)->update($data);
    }

    public function deleteByOrderIds(array $fbLogOrderIds)
    {
       return Db::name($this->table)->where('order_id', 'in', $fbLogOrderIds)->delete();
    }


}